package com.codeplex.jtfscli.commands;

import java.util.List;
import java.util.Map;

import com.codeplex.jtfscli.util.TFSConnectionFactory;
import com.microsoft.tfs.core.clients.versioncontrol.VersionControlClient;

public class WorkspaceCommand extends Command {

	public WorkspaceCommand(TFSConnectionFactory connectionFactory) {
		super(connectionFactory);
	}

	public WorkspaceCommand() {
		super();
	}

	public static String getOwner(final Map<String, String> parsedArgs,
			final List<String> regularArgs) {
		if (parsedArgs.containsKey("-owner"))
			return parsedArgs.get("-owner");
		else if (regularArgs.size() == 0)
			return null;
		else if (regularArgs.get(0).indexOf(';') != -1) {
			final String ret = regularArgs.get(0).substring(
					regularArgs.get(0).indexOf(';') + 1);
			return ret.equals("") ? null : ret;
		}
		return null;
	}

	@Override
	public void run(final Map<String, String> parsedArgs,
			final List<String> regularArgs, final String username,
			final String password) {
		final String workspaceName = regularArgs.get(0);
		if (parsedArgs.containsKey("-new"))
			newWorkspace(parsedArgs.get("-server"), username, password,
					parsedArgs.get("-comment"), workspaceName.substring(
							0,
							workspaceName.indexOf(';') == -1 ? workspaceName
									.length() : workspaceName.indexOf(';')),
					getOwner(parsedArgs, regularArgs));
		else if (parsedArgs.containsKey("-delete"))
			deleteWorkspace(parsedArgs.get("-server"), username, password,
					workspaceName.substring(
							0,
							workspaceName.indexOf(';') == -1 ? workspaceName
									.length() : workspaceName.indexOf(';')),
					getOwner(parsedArgs, regularArgs));
	}

	/**
	 * Creates a new workspace.
	 * 
	 * @param server
	 *            The server to connect to.
	 * @param usernameAndDomain
	 *            The username with which to login to the server.
	 * @param password
	 *            The password with which to login to the server.
	 * @param comment
	 *            The comment to associate with the workspace (may be null).
	 * @param workspaceName
	 *            The name of the workspace.
	 * @param workspaceOwner
	 *            The owner of the workspace(may be null).
	 */
	public void newWorkspace(final String server,
			final String usernameAndDomain, final String password,
			final String comment, final String workspaceName,
			final String workspaceOwner) {
		connectionFactory.createConnection(server, usernameAndDomain, password)
				.getVersionControlClient()
				.createWorkspace(null, workspaceName, workspaceOwner, comment);
	}

	/**
	 * Deletes the workspace on the given server for given user as defined by
	 * the given name (and optionally, owner).
	 * 
	 * @param server
	 *            The server on which the workspace should be deleted.
	 * @param usernameAndDomain
	 *            The username to use when connecting to the server.
	 * @param password
	 *            The password to use when connecting to the server.
	 * @param workspaceName
	 *            The name of the workspace to delete.
	 * @param workspaceOwner
	 *            The owner of the workspace to delete (may be null).
	 */
	public void deleteWorkspace(final String server,
			final String usernameAndDomain, final String password,
			final String workspaceName, final String workspaceOwner) {
		final VersionControlClient client = connectionFactory.createConnection(
				server, usernameAndDomain, password).getVersionControlClient();
		client.deleteWorkspace(client.queryWorkspace(workspaceName,
				workspaceOwner));
	}
}